package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private QuestionService questionService;

    @Reference
    private FriendService friendService;

    @Reference
    private CommentService commentService;

    // 聊一下（回复陌生人问题）
    public void liaoYiXia(long jiarenId, String reply) {
        // 1.获取登录userId
        Long userId = UserHolder.get().getId();
        // 2.查询你的昵称
        String nickname = userInfoService.findByUserId(userId).getNickname();
        // 3.查询对方问题
        Question question = questionService.findByUserId(jiarenId);
        if (question == null) {
            question = new Question();
            question.setStrangerQuestion("什么是快乐星球？");
        }
        // 4.组装参数
        Map<String, String> param = new HashMap<>();
        param.put("userId", String.valueOf(userId));
        param.put("nickname", nickname);
        param.put("strangerQuestion", question.getStrangerQuestion());
        param.put("reply", reply);
        String json = JSON.toJSONString(param);
        // 5.发送给环信
        huanXinTemplate.sendMsg(String.valueOf(jiarenId), json);

    }

    // 添加好友
    public void addContacts(Long friendId) {
        // 1.获取登录userId
        Long userId = UserHolder.get().getId();
        // 2.mongo建立好友
        friendService.addContacts(userId, friendId);
        // 3.环信建立好友
        huanXinTemplate.addContacts(userId, friendId);
    }

    // 查询好友列表
    public ResponseEntity findContacts(Integer pageNum, Integer pageSize) {
        // 1.获取登录userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        PageBeanVo pageBeanVo = friendService.findContacts(userId, pageNum, pageSize);
        // 3.封装vo
        List<ContactVo> voList = new ArrayList<>();
        // 3-1 获取好友列表
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
        // 3-2 遍历好友列表
        if (CollectionUtil.isNotEmpty(friendList)) {
            for (Friend friend : friendList) {
                // 创建vo
                ContactVo vo = new ContactVo();
                UserInfo userInfo = userInfoService.findByUserId(friend.getFriendId());
                vo.setUserInfo(userInfo); // 好友信息
                vo.setUserId(friend.getFriendId().toString());// 好友id

                // 添加集合中
                voList.add(vo);

            }
        }

        // 4.重置vo到分页对象
        pageBeanVo.setItems(voList);

        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 根据类型查询用户信息
    public ResponseEntity findUserCommentVo(Integer commentType, Integer pageNum, Integer pageSize) {
        // 1.获取登录userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        PageBeanVo pageBeanVo = commentService.findCommentUserByPage(userId, commentType, pageNum, pageSize);
        // 3.封装vo
        List<UserCommentVo> voList = new ArrayList<>();
        // 3-1 取出comment集合
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        // 3-2 遍历
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 创建vo
                UserCommentVo vo = new UserCommentVo();
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
                vo.setId(userInfo.getId().toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 操作时间（点赞、喜欢、评论）

                // 添加到集合
                voList.add(vo);
            }
        }

        // 4.重置vo到分页对象
        pageBeanVo.setItems(voList);
        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }
}
